package inf.coach.mobile;

import java.util.Locale;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView.OnEditorActionListener;

public class Crossing extends Activity {
	
    public static long userid = 0;
    public static long osmid= 0;
    public static double lat;
    public static double lon;
    public RadioGroup mRadioGroup; 
    public RadioButton b1;
    public RadioButton b2;
    public RadioButton b3;
    public RadioButton b4;
	 public String type;
    
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.crossing);
        setTitle("funktion Seite, Kreuzung Informationen");
         
       	final Bundle bundle = this.getIntent().getExtras();  
		userid = bundle.getLong("userid");
		osmid= bundle.getLong("osmid");
		lat=bundle.getDouble("lat");
		lon=bundle.getDouble("lon");
		
		final boolean flag =bundle.getBoolean("flag");
		
        final Button send=(Button) findViewById(R.id.button_crossing_sendbutton);
        final EditText name=(EditText) findViewById(R.id.editText_crossing_name);
        final CheckBox insideisland=(CheckBox)findViewById(R.id.checkBox_crossing_insideisland);
        final CheckBox ampel=(CheckBox)findViewById(R.id.checkBox_crossing_ampel);        
        final CheckBox insel=(CheckBox)findViewById(R.id.checkBox_crossing_insel);
        mRadioGroup= (RadioGroup)  findViewById(R.id.radioGroup_crossing);
		
        b1=(RadioButton) findViewById(R.id.radio_crossing_x);
        b2=(RadioButton) findViewById(R.id.radio_crossing_y);
        b3=(RadioButton) findViewById(R.id.radio_crossing_t);
        b4=(RadioButton) findViewById(R.id.radio_crossing_others);
		
        final Button direction=(Button) findViewById(R.id.button_crossing_direction);
		
		if(flag)
		{

			name.setText(bundle.getString("streetname"));
			name.setEnabled(false);
			insideisland.setChecked(bundle.getBoolean("insideisland"));
			insideisland.setClickable(false);
			ampel.setChecked(bundle.getBoolean("trafficlight"));
			ampel.setClickable(false);
			insel.setChecked(bundle.getBoolean("roundabout"));
			insel.setClickable(false);
			if(bundle.getString("type").equals("X type"))
			{
				b1.setChecked(true);
			}
			if(bundle.getString("type").equals("Y type")){
				b2.setChecked(true);
			}
			if(bundle.getString("type").equals("T type")){
				b3.setChecked(true);
			}
			if(bundle.getString("type").equals("Others")){
				b4.setChecked(true);
			}
			mRadioGroup.setEnabled(false);
			b1.setEnabled(false);
			b2.setEnabled(false);
			b3.setEnabled(false);
			b4.setEnabled(false);
		}  
		else
		{
			send.setText("Absenden");
			send.setContentDescription("Button, absenden ");
		}
		
     
        send.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View arg0){
        		if(flag && send.getText().equals("Bearbeiten"))
        		{
        			name.setEnabled(true);
        			insideisland.setClickable(true);
        			ampel.setClickable(true);
        			insel.setClickable(true);
        			mRadioGroup.setEnabled(true);
        			b1.setEnabled(true);
        			b2.setEnabled(true);
        			b3.setEnabled(true);
        			b4.setEnabled(true);
        			direction.setText("GPS-Informationen erhalten");
        			direction.setContentDescription("Button, GPS-Informationen erhalten ");
        			send.setText("Absenden");
        			send.setContentDescription("Button, absenden ");
        		}
        		else if(flag && send.getText().equals("Absenden"))
        		{
        			if(b1.isChecked()){
        				type="X type";
        			}
        			if(b2.isChecked()){
        				type="Y type";
        			}
        			if(b3.isChecked()){
        				type="T type";
        			}
        			if(b4.isChecked()){
        				type="Others";
        			}
            		if(ConnectionReceiver.isInternetConnected(getApplicationContext()))
    				{
    									
    				String text = webservice.updateCrossingAnno(userid, osmid, 0.0, 0.0, name.getTag().toString(), insideisland.isChecked(), ampel.isChecked(), insel.isChecked(),type);
    				System.out.println(text);
    				if (!text.equals(null) && text.equals("successful")) {
    					message.show(Crossing.this,"Fehlgeschlagen", "Annotation updated");

    				}
    				if (text.equals(null) || text.equals("failed")) {
    					message.show(Crossing.this,"failed", "Bitte überprüfen Internetverbindung!");
    				}
    				}
            			else{
        					message.show(Crossing.this,"Fehler", "Keine Internetverbindung! ");
        				}
        		}
        		else
        		{
        		if(ConnectionReceiver.isInternetConnected(getApplicationContext()))
				{
									
				String text = webservice.createCrossingAnno(userid, osmid, 0.0, 0.0, name.getText().toString(), insideisland.isChecked(), ampel.isChecked(), insel.isChecked(),type);
				System.out.println(text);
				if (!text.equals(null) && text.equals("successful")) {
					message.show(Crossing.this,"success", "Annotation created");

				}
				if (text.equals(null) || text.equals("failed")) {
					message.show(Crossing.this,"Fehlgeschlagen", "Bitte überprüfen Internetverbindung!");
				}
				}
        			else{
    					message.show(Crossing.this,"Fehler", "Keine Internetverbindung !");
    				}
        		}
        	}
        	
        });
       direction.setOnClickListener(new Button.OnClickListener(){
    	   public void onClick(View arg0){
    		   if(ConnectionReceiver.isInternetConnected(getApplicationContext()))
				{
    			   Intent intent = new Intent();
					Bundle bundle = new Bundle();
					bundle.putDouble("lat", lat);
					bundle.putDouble("lon",lon);
					intent.setClass(Crossing.this, Orientation.class);
					intent.putExtras(bundle);
					startActivity(intent);
				}
    		   else
    		   {
    			   message.show(Crossing.this,"Fehler", "Keine Internetverbindung! ");
    		   }
       }
       });
       
     
	}
    protected void dialoggoback() { 
        AlertDialog.Builder builder = new Builder(Crossing.this); 
        builder.setMessage("Möchten Sie zur letzten Seite zurück springen?"); 
        builder.setTitle("Information"); 
        builder.setPositiveButton("ok", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                        Crossing.this.finish(); 
                    }

                }); 
        builder.setNegativeButton("abbrechen", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                    } 
                }); 
        builder.create().show(); 
    } 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
            dialoggoback(); 
            return false; 
        } 
        return false; 
    } 
	
}
